<template>
  <el-form
      label-position="right"
      label-width="100px"
      :model="formdata"
      style="max-width: 460px"
  >
    <el-form-item label="用户名">
      <el-input v-model="formdata.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input
          v-model="formdata.password"
          type="password"
          show-password/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from "axios";
import qs from "qs";

export default {
  name: 'LoginView',
  data() {
    return {
      formdata: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onSubmit() {
      axios({
        url: "/api/login",
        method: "post",
        data: qs.stringify(this.formdata)
      }).then(res => {
        if (res.data.success) {
          // 将用户信息存储到vuex里

          // 进行路由跳转
          this.$router.push("/home");
        } else {
          // 提示错误
          // 可使用element-plus显示一个提示框
        }
      }).catch(err => {
      }).then(() => {
      })
    }
  }
}
</script>

<style lang="scss">

</style>